package entity

import (
	"fmt"
	"strconv"
	"strings"
	"time"

	"gorm.io/gorm"
	"shqsoft.cn/pas3ui/base"
)

// Zxjl
type Zxjl struct {
	ID   int       `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	NFJD time.Time `gorm:"column:NFJD"`
	BMID int       `gorm:"column:BMID"`
	BMMC string    `gorm:"column:BMMC"`
	EMNO string    `gorm:"column:EMNO"`
	EMNM string    `gorm:"column:EMNM"`
	EMID int       `gorm:"column:EMID"`
	ZBID int       `gorm:"column:ZBID"`
	JLMC string    `gorm:"column:JLMC"`
	JLJE float64   `gorm:"column:JLJE"`
	DRID int       `gorm:"column:DRID"`
}

// TableName
func (Zxjl) TableName() string {
	return "SH_JHCW_ZXJL"
}

// BeforeSave
func (u *Zxjl) BeforeSave(tx *gorm.DB) (err error) {
	var emid, dpid, pdid *int
	var dpnm *string
	var emno, emnm string
	jdint := datenfjj(u.NFJD)
	err = can_import(tx, jdint)
	if err != nil {
		return
	}
	tx.Raw(`select "EMID","EMNM" from "SH_JHCW_JXFF_YGJX" where "FFJD"=? AND "EMNO" = ?`, jdint, u.EMNO).Row().Scan(&emid, &emnm)
	if emid == nil {
		err = tx.Raw(`select "DPID","DPNM","EMID","EMNO","EMNM","PDID" from "V_USER_DEPT" where "EMNO"=?`, u.EMNO).Row().Scan(&dpid, &dpnm, &emid, &emno, &emnm, &pdid)
		if err != nil || emid == nil {
			err = fmt.Errorf("员工号%s在类别绩效或绩效系统中不存在", u.EMNO)
			return
		}
		err = tx.Exec(`insert into "SH_JHCW_JXFF_YGJX" ("FFJD","DPID","DPNM","EMID","EMNO","EMNM","PDID") values(?,?,?,?,?,?,?)`, jdint, dpid, dpnm, emid, emno, emnm, pdid).Error
		if err != nil {
			return
		}
	}
	emnm_sys := strings.TrimSpace(emnm)
	emnm_imp := strings.TrimSpace(u.EMNM)
	if !strings.EqualFold(emnm_sys, emnm_imp) && !strings.Contains(emnm_sys, emnm_imp) {
		err = fmt.Errorf("员工号%s对应的姓名是%s,而提供姓名是%s", u.EMNO, emnm, u.EMNM)
		return
	}
	tx.Raw(`select "DPNM","DPID"  from "SH_JHCW_JXFF_YGJX" where "EMNO" = ? AND  "FFJD"=? `, u.EMNO, jdint).Row().Scan(&dpnm, &dpid)
	if dpid == nil {
		err = fmt.Errorf("部门%s在类别绩效中不存在,或者类别绩效没有导入", u.BMMC)
		return
	}
	u.BMID = *dpid
	u.BMMC = *dpnm
	u.EMID = *emid
	return
}

// //LbjxAfterImport 导入后执行
func ZxjlAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	var jlmc *string
	ffjd := params["NFJD"]
	zbid, err := strconv.Atoi(params["ZBID"])
	if err != nil {
		return
	}
	var date time.Time
	date, err = time.Parse("2006-01-02", ffjd)
	if err != nil {
		return
	}
	quarter := datenfjj(date)

	var sqlstr string
	tx.Raw(`select "JLMC" from "SH_JHCW_ZXJL"  WHERE "NFJD" =? and "ZBID" = ? and "ISOLD"= 0`, ffjd, zbid).Row().Scan(&jlmc)
	if jlmc != nil {
		sqlstr = `DELETE FROM  "SH_JHCW_ZXJL" WHERE "NFJD" =? and "ZBID" =? and "JLMC" = ? and "ISOLD" = 1`
		err = tx.Exec(sqlstr, ffjd, zbid, jlmc).Error
		if err != nil {
			return
		}
	}
	sqlstr = `UPDATE "SH_JHCW_ZXJL" SET "ISOLD" = 1 WHERE "NFJD" =? and "ZBID" =? and "JLMC" = ? `
	err = tx.Exec(sqlstr, ffjd, zbid, jlmc).Error
	if err != nil {
		return
	}
	sqlstr = `UPDATE "SH_JHCW_JXFF_YGJX" SET "YWJX_ZXJL" = 0 WHERE "FFJD" = ?`
	err = tx.Exec(sqlstr, quarter).Error
	if err != nil {
		return
	}
	if base.AppConf.Database.Mysql != "" {
		sqlstr = `UPDATE "SH_JHCW_JXFF_YGJX" A,(select "EMNO","EMID",sum("JLJE") "JLJE" from "SH_JHCW_ZXJL" where "NFJD"=? group by "EMNO","EMID") B
		set A."EMNO" = B."EMNO",A."EMID" = B."EMID","YWJX_ZXJL" = B."JLJE"
		WHERE A."FFJD" = ? AND A."EMNO" = B."EMNO"`
	} else {
		sqlstr = `UPDATE "SH_JHCW_JXFF_YGJX" A
		set "EMNO" = B."EMNO","EMID" = B."EMID","YWJX_ZXJL" = B."JLJE"
		FROM (select "EMNO","EMID",sum("JLJE") "JLJE" from "SH_JHCW_ZXJL" where "NFJD"=? group by "EMNO","EMID") B
		WHERE A."FFJD" = ? AND A."EMNO" = B."EMNO"`
	}
	err = tx.Exec(sqlstr, ffjd, quarter).Error
	if err != nil {
		return
	}
	err = ImpJxffAfter(tx, quarter)
	return
}

func ZxjlEF() base.EntityFunc {
	return func() interface{} {
		entity := Zxjl{}
		return &entity
	}
}
func ZxjlESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []Zxjl
		return &entities
	}
}

// Zxjezb
type Zxjezb struct {
	ZBID int    `gorm:"column:ZBID"`
	ZBMC string `gorm:"column:ZBMC"`
}

// TableName
func (Zxjezb) TableName() string {
	return "SH_JHCW_ZXJL_ZB"
}

func ZxjezbEF() base.EntityFunc {
	return func() interface{} {
		entity := Zxjezb{}
		return &entity
	}
}
func ZxjezbESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []Zxjezb
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("Zxjl", base.NewDefaultDao(ZxjlEF(), ZxjlESF(), nil, "my"))
	base.DaoPoolObj.Put("Zxjezb", base.NewDefaultDao(ZxjezbEF(), ZxjezbESF(), nil, "my"))
	base.SetAfterCallBack("Zxjl", ZxjlAfterImport)
}
